package com.xuecheng.content.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.xuecheng.commons.model.dto.BindTeachplanWorkDto;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.TeachplanWork;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanWorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class TeachplanWorkController {

    @Autowired
    private TeachplanWorkService teachplanWorkService;

    @Autowired
    private CourseBaseService courseBaseService;

    //课程计划绑定作业
    @PostMapping("/teachplan/work/association")
    public ResponseResult association(@RequestBody BindTeachplanWorkDto bindDto){
        return teachplanWorkService.association(bindDto);
    }

    //课程计划解绑作业
    @DeleteMapping("/teachplan/work/{teachplanWorkId}")
    public ResponseResult deleteWork(@PathVariable("teachplanWorkId")Long teachplanWorkId){
        return teachplanWorkService.deleteWork(teachplanWorkId);
    }

    //根据workId查课程标题
    @GetMapping("/teachplan/work")
    public ResponseResult<String> findTitleByWorkId(@RequestParam Long workId){
        List<TeachplanWork> teachplanWorkList = teachplanWorkService.lambdaQuery().eq(TeachplanWork::getWorkId, workId).list();
        if (CollectionUtil.isEmpty(teachplanWorkList)){
            return ResponseResult.okResult(null);
        }
        TeachplanWork teachplanWork = teachplanWorkList.get(0);
        Long courseId = teachplanWork.getCourseId();
        CourseBase courseBase = courseBaseService.getById(courseId);
        String courseBaseName = courseBase.getName();
        return ResponseResult.okResult(courseBaseName);
    }
}
